// 公共方法注释补充，三元表达式优化
export default [
  {
    name: "date-format",
    fn(value) {
      return value ? new Date(value).Format("yyyy-MM-dd") : "--";
    }
  },
  {
    name: "selection-disabled",
    /**
     * 状态为有效：禁用复选框，不可删除
     * @param {Object[]} data 
     * @param {string} data[].enabled - 是否有效
     * @param {string} data[].isAvailable - 是否可见
     * @returns {Object[]}
     */
    fn(data) {
      if (data && data.length != 0) {
        data = data.map(item => {
          if (item.enabled == 1 || item.isAvailable == 1) {
            item._disabled = true;
          }
          return item;
        });
      }
      return data;
    }
  },
  {
    // 判断当前时间是否在传入的有效时间内
    name: "effective-time",
    fn(beginTime, endTime) {
      let time = new Date();
      let Y = time.getFullYear();
      let M = time.getMonth() + 1;
      let D = time.getDate();
      let h = time.getHours();
      let m = time.getMinutes();
      let s = time.getSeconds();
      if (M < 10) M = "0" + M;
      if (D < 10) D = "0" + D;
      if (h < 10) h = "0" + h;
      if (m < 10) m = "0" + m;
      if (s < 10) s = "0" + s;
      let curDate = Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
      let effectiveTime = ''                      
      if( beginTime && endTime){
          effectiveTime = (curDate >= beginTime && curDate <= endTime)
      }
      if(beginTime){
         effectiveTime = (curDate >= beginTime)
      }
      if(endTime){
         effectiveTime = (curDate <= endTime)
      }else{
        effectiveTime = true
      }
      
      return  effectiveTime;
    }
  },
  {
    // 排序：将已选应用排于未选应用前
    name: "sort-checkedAppFirst",
    fn(data) {
      let checkedArr = [];
      let unCheckedArr = [];
      for (var i = 0; i < data.length; i++) {
        if (data[i]._checked && data[i]._checked == true) {
          checkedArr.push(data[i]);
        } else {
          unCheckedArr.push(data[i]);
        }
      }
      let sortResult = checkedArr.concat(unCheckedArr);
      return sortResult;
    }
  },
  {
    name: "checkedAppFirst",
    fn(allData, selectedData) {
      const selectedCode = selectedData.map(item => item.applicationCode)
      //已选项
      const selected = allData.filter(item => selectedCode.indexOf(item.applicationCode) != -1)
      const unSelected = allData.filter(item => selectedCode.indexOf(item.applicationCode) == -1)
      return selected.concat(unSelected)
    }
  }
];
